﻿
@{
    ViewBag.Title = "样品管理";
}

<div class="input-group">
    <div class="row col-md-12 ml-0" style="min-height:5vh;">
        <div class="row col-md-8 ml-0" style="min-height:5vh;">
            <div class="col-3 pr-0" style="display: none">
                <select id="SampleManager-Location"></select>
            </div>
            <strong class="font-rfs" style="margin-right: 2rem;padding-top: 0.4rem;">样品管理</strong>
            <select id="SampleManager-Current" class="form-control font-rfs border-radius-noradius noRightBorder custom-headtoolbar-select" style="width:10%;"></select>
            <select id="SampleManager-SelectType" class="form-control font-rfs border-radius-noradius noRightBorder custom-headtoolbar-select" style="width:10%;">
                <option></option>
                <option>批次号</option>
                <option>样品ID</option>
                <option>样品编号</option>
            </select>
            <input id="SampleManager-Name" class="btn btn-rfs border-radius-noradius" style="border: 1px solid rgba(0,0,0,.15);height: calc(1.5em + .75rem + 1px) !important;width:15% !important;" onkeydown="SearchByType(this)" />
            <button id="SampleManager-Export" class="btn btn-rfs" title="导出EXCEL"><i class="bi-filetype-xlsx" style="color:forestgreen"></i></button>
            <button id="SampleManager-Import" class="btn btn-rfs" title="导入EXCEL"><i class="bi-filetype-xlsx" style="color:fuchsia"></i></button>
            <button id="SampleManager-Abandon" class="btn btn-rfs"><i class="bi-x-circle" style="color:red"></i>废弃</button>
            <button id="SampleManager-Report" class="btn btn-rfs" title="生成单样报告"><i class="bi-printer-fill"></i>报告</button>
        </div>
        <div class="row col-md-4 ml-0" style="min-height:5vh;">
            <strong class="font-rfs" style="margin-right: 1rem;padding-top: 0.4rem;color:forestgreen;">批量处理</strong>
            <a href="#menuReport" class="btn btn-rfs" data-toggle="dropdown">批量报告<i class="bi-caret-down-fill" style="color:forestgreen;"></i></a>
            <div id="menuReport"><ul id="ulmenuReport" class="dropdown-menu"></ul></div>
            <a href="#OriginalRecords" class="btn btn-rfs" data-toggle="dropdown">原始记录<i class="bi-caret-down-fill" style="color:forestgreen;"></i></a>
            <div id="OriginalRecords"><ul id="ulOriginalRecords" class="dropdown-menu"></ul></div>
            <button id="ExamineAndVerify-ManagerFiles" class="btn btn-rfs"><i class="bi-file-earmark-arrow-up-fill" style="color:#007bff"></i> 附件</button>
        </div>
    </div>
</div>
<div class="row col-md-12 ml-0 pre-scrollable" style="min-height:90vh;">
    <div class="row col-md-12" style="padding-right:0px;left:15px;padding-left: 0px;min-height: 40vh;">
        <div class="input-group">
            <div class="divsample row col-md-8" style="padding-right:0px;left:15px;padding-left: 0px;min-height: 40vh;">
                <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-12 pre-scrollable" style="min-height:40vh;height:40vh;">
                    <table id="SampleManager-Table" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th></th>
                                <th>样品ID</th>
                                <th>批次号</th>
                                <th>样品编号</th>
                                <th>名称</th>
                                <th>样品描述</th>
                                <th>类型</th>
                                <th>分类</th>
                                <th>采样点</th>
                                <th>指派人</th>
                                <th>指派组</th>
                                <th>附件</th>
                                <th>状态</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
            </div>
            </div>
            <div class="divsample row col-md-4" style="padding-left:0px;padding-right:0px;left:45px;min-height: 40vh;">
                <div class="divsample table-responsive-sm shadow p-0 bg-white rounded col-md-12 pre-scrollable" style="min-height:40vh;">
                    <table id="SampleManager-BatchTable" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>批次号</th>
                                <th>Ex1</th>
                                <th>Ex2</th>
                                <th>Ex3</th>
                                <th>部门/单位</th>
                                <th>送样人</th>
                                <th>送样时间</th>
                                <th>登录人</th>
                                <th>登录时间</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="row col-md-12 ml-0" style="margin-top: 7px; padding-right:0px;padding-left: 0px;min-height: 49vh;">
        <div class="row col-md-12 tab-content" style="padding-left: 0;padding-right: 0px;right: -15px;min-height: 3vh;">
            <ul class="nav col-md-12" style="padding-right: 0;margin-bottom: 1px;">
                <li id="li-tabAnalyte" class="tabButton col-md-1 tab-first-active font-rfs" style="text-align:center;padding-left: 1px;padding-right: 1px;" data-active="true">
                    <a id="a-tabAnalyte" href="#tabAnalyte" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">查看结果</a>
                </li>
                <li id="li-tabHistory" class="tabButton col-md-1 tab-other-noactive font-rfs" style="text-align: center;padding-left: 1px;padding-right: 1px;" data-active="false">
                    <a id="a-tabHistory" href="#tabHistory" data-toggle="tab" class="text-decoration-none" style="display: inline-block;width:100%;">历史记录</a>
                </li>
                <li class="col-md-10 tab-Notactive" style="text-align: center;"><a href="" data-toggle="tab"></a></li>
            </ul>
        </div>
        <div class="row col-md-12 tab-content tab-body" style="padding-right: 0px;right: -15px;padding-left: 0px;top: -2px;min-height: 46vh;">
            <div class="col-md-12 ml-0 mr-0 tab-pane active" id="tabAnalyte" style="padding-left: 0px;padding-right: 0px;">
                <div class="row col-md-12" style="min-height:2vh;">
                    <button id="SampleManager-Review" class="btn btn-rfs"><i class="bi-arrow-repeat" style="color:red"></i> 重测</button>
                </div>
                <div class="table-responsive-sm p-0 bg-white rounded col-md-12 pre-scrollable" style="min-height:30vh">
                    <table id="SampleManager-TabAnalyts" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>样品ID</th>
                                <th>样品编号</th>
                                <th>测试名称</th>
                                <th>代码</th>
                                <th>名称</th>
                                <th>修约</th>
                                <th>-3σ</th>
                                <th>-2σ</th>
                                <th>Std</th>
                                <th>+2σ</th>
                                <th>+3σ</th>
                                <th>原始结果</th>
                                <th>单位</th>
                                <th>最终结果</th>
                                <th>单位</th>
                                <th>结论</th>
                                <th>重测</th>
                                <th>检测</th>
                                <th>测试</th>
                                <th>模式</th>
                                <th>设备</th>
                                <th>来源</th>
                                <th>分析者</th>
                                <th>时间</th>
                                <th>状态</th>
                                <th>过滤</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
            <div class="col-md-12 ml-0 mr-0 tab-pane" id="tabHistory" style="padding-left: 0px;padding-right: 0px;">
                <div class="table-responsive-sm p-0 bg-white rounded col-md-12" style="min-height:41vh">
                    <table id="SampleManager-History" class="table table-striped table-bordered table-sm table-hover">
                        <thead>
                            <tr>
                                <th>样品ID</th>
                                <th>流程步骤</th>
                                <th>步骤状态</th>
                                <th>进入时间</th>
                                <th>处理人</th>
                                <th>处理时间</th>
                                <th>处理意见</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="SampleManager-Modal" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="SampleManager-Modal-Title" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <div id="SampleManager-Modal-Embed"></div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="SampleManager-Abandon-Modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">废弃样品</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p class="font-rfs">请输入废弃样品的原因：</p>
                <p><textarea id="modal-Suggestion" class="font-rfs" rows=2 style="width:100%;"></textarea></p>
            </div>
            <div class="modal-Delete-footer">
                <button id="modal-DoAbandon" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">确定</button>
                <button id="cancleDoAbandon" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    
    <script>
        var ResultId = "";
        var SampleId = "";

        $(document).ready(function () {
            GetLocationList('SampleManager-Location', '采样点');
            getGlobalsContainName('ulmenuReport', '批量报告');
            getGlobalsContainName('ulOriginalRecords', '批量原始记录');
            LoadBatchByResult('SampleManager-Current');
        });

        function setAttachment(value, row, index) {
            var strHtml = "<button class='table-cell-button btn-rfs' data-Method-Id=" + row.id + " onclick='TableCellButton(this)'><i class='bi-tools' title='附件管理'></i></button>";
            return strHtml;
        }

        function TableCellButton(obj) {
            $("#SampleManager-Modal-Title").text("样品附件管理");

            $("#SampleManager-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("/Common/ManageFile", {
                Domain: "Sample",
                DomainId: $(obj).parent().parent().attr('data-uniqueid'),
                ModalID: "SampleManager-Modal"
            }, function (datas) {
                $("#SampleManager-Modal-Embed").html(datas);
            });
            event.stopPropagation();
        }

        function SetRelease(value, row, index) {
            var strHtml = "";
            if (row.isRelease) {
                strHtml = "<input type='checkbox' checked onclick='ReleaseOnClick(this)' data-rowid='" + row.id + "'/>";
            } else {
                strHtml = "<input type='checkbox' onclick='ReleaseOnClick(this)' data-rowid='" + row.id + "'/>";
            }
            return strHtml;
        }

        function ReleaseOnClick(obj) {
            var st = $(obj).prop("checked");
            var rowid = $(obj).attr("data-rowid");

            $.post("UpdateSampleLoginIsRelease", {
                SampleLoginId: rowid,
                IsRelease: st
            }, function (data) {
                if (data.indexOf("成功") < 0) {
                    alert(data);
                }
            });
        }

        function SearchByType(obj) {
            if (event.keyCode === 13) {
                var type = $("#SampleManager-SelectType").find("option:selected").text();
                if (type.length == 0) { 
                    alert("请先选择需要查询的类别！");
                }
                LoadSamples(type, $(obj).val());

                if (type = "批次号") {
                    LoadSampleBatch($(obj).text());
                }
            }
        }

        function LoadSamples(type, value) {
            $('#SampleManager-Table').bootstrapTable('destroy');
            SampleId = "";
            var columns = [{ field: "id", visible: false }, { field: "selectItem", width: "5%", align: "center", checkbox: true },
            { field: "sampleId"}, { field: "batchId", formatter: SetBatchId }, { field: "sampleNumber", formatter: SetSampleNumber }, { field: "name" },
            { field: "description" }, { field: "classification" }, { field: "genricClass" }, { field: "samplingPoint", formatter: setSelect },
            { field: "assignPersonName" }, { field: "assignGroupName" }, { field: "SopFiles", width: "5%", formatter: setAttachment },
            { field: "status", width: "5%", align: "center", formatter: SetSampleStatus }];

            $('#SampleManager-Table').bootstrapTable({
                method: 'post',
                url: "GetSampleBySearch",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset, // 偏移量
                        Type: type,
                        value: value
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 20,   // 默认页大小
                pageList: [10, 20,30, 50, 100],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,

                selectItemName: "selectItem", // 指定选择行ID
                clickToSelect: true, // 相应单击，自动选中此行
                checkboxHeader: true, // 标题行显示全选按钮

                onClickRow: function (row, $element, field) {
                    $("#SampleManager-Table").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    SampleId = row.id;
                    LoadSingleResult(row.id);
                    LoadSampleBatch(row.batchId);

                    if ($("#li-tabHistory").attr("data-active") == 'true') { // 历史记录选项卡不是活动状态则不加载数据
                        LoadHistory(SampleId);
                    }
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#SampleManager-Table').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        function SetSampleNumber(value, row, index) {
            var text = value == null ? '' : value;
            var strHtml = "<input class='table-cell-input' data-Id=" + row.id + " onclick='javascript:{event.stopPropagation();}' onchange='UpdateSampleNumber(this)' value='" + text + "'/>";
            return strHtml;
        }

        function SetBatchId(value, row, index) {
            var text = value == null ? '' : value;
            var strHtml = "<input class='table-cell-input' data-Id=" + row.id + " onclick='javascript:{event.stopPropagation();}' onchange='UpdateBatchId(this)' value='" + text + "'/>";
            return strHtml;
        }

        function UpdateSampleNumber(obj) {
            var st = $(obj).val();
            if (st.length > 50)
                alert("字符串长度不能超过50，多出部分将自动被截取！");

            $.post("postUpdateSampleLoginSampleNumber", {
                Id: $(obj.outerHTML).attr("data-Id"),
                SampleNumber: st
            }, function (data) {
                if (data.indexOf("成功") <= 0) {
                    alert(data);
                }
            });
        }

        function UpdateBatchId(obj) {
            var st = $(obj).val();
            if (st.length > 50)
                alert("字符串长度不能超过50，多出部分将自动被截取！");

            $.post("postUpdateSampleLoginBatchId", {
                Id: $(obj.outerHTML).attr("data-Id"),
                BatchId: st
            }, function (data) {
                if (data.indexOf("成功") <= 0) {
                    alert(data);
                }
            });
        }

        function setSelect(value, row, index) {
            var strHtml = "<select class='table-cell-select' onclick='javascript:{event.stopPropagation();}' onchange='UpdateSamplingPoint(this)'>";
            var array = $('#SampleManager-Location option').toArray();
            $(array).each(function () {
                if (value == $(this).html()) {
                    strHtml = strHtml + "<option selected='true'>" + $(this).html() + "</option>";
                } else {
                    strHtml = strHtml + "<option>" + $(this).html() + "</option>";
                }
            })
            return strHtml + "</select>";
        }

        function UpdateSamplingPoint(obj) {
            $.post("UpdateSamplingPoint", {
                SampleId: $(obj).parent().parent().attr('data-uniqueid'),
                SamplingPoint: $(obj).val()
            }, function (data) {
                //alert(data);
            });
        }

        function SetStatus(value, row, index) {
            var strHtml = "-";
            if (value == "Done") {
                strHtml = "<i class='bi bi-check-circle-fill'></i>";
            }
            if (value == "OOG") {
                strHtml = "<i class='bi bi-exclamation-circle-fill'></i>";
            }
            if (value == "OOS") {
                strHtml = "<i class='bi bi-x-circle-fill'></i>";
            }
            return strHtml;
        }

        function SetSampleStatus(value, row, index) {
            var strHtml = "-";
            if (value == "完成") {
                strHtml = "<i class='bi bi-check-circle-fill'></i>";
            }
            if (value == "检测中") {
                strHtml = "<i class='bi bi-arrow-right-circle-fill'></i>";
            }
            if (value == "废弃") {
                strHtml = "<i class='bi bi-x-circle-fill'></i>";
            }
            return strHtml;
        }

        function CallRecordSheet(obj) {
            var id = $(obj).parent().parent().attr('data-uniqueid');

            $.post("CallRecordSheet", {
                ResultId: id
            }, function (datas) {
                newWindow = window.open();
                newWindow.jsPDF = window.jspdf.jsPDF;
                newWindow.document.write(datas);
            });
        }

        function UpdateFinal(obj) {
            var result = $(obj).val();
            var id = $(obj).parent().parent().attr('data-uniqueid');

            $.post("ManualResult", {
                Id: id,
                Original: result
            }, function (datas) {
                for (var i = 0; i < datas.length; i++) {
                    var row = $('#SampleManager-TabAnalyts').bootstrapTable('getRowByUniqueId', datas[i].id); // 获得行数据
                    row.original = datas[i].original;
                    row.final = datas[i].final;
                    row.status = datas[i].status;
                    row.conclusion = datas[i].conclusion;
                    $('#SampleManager-TabAnalyts').bootstrapTable('updateByUniqueId', row);  // 更新行数据

                    var index = parseInt($(obj).parent().parent().attr('data-index'));
                    $("#SampleManager-TabAnalyts").find("tbody").find("tr").eq(index).removeClass('changeblueColor');
                    $("#SampleManager-TabAnalyts").find("tbody").find("tr").eq(index + 1).addClass("changeblueColor");
                    $("#SampleManager-TabAnalyts").find("tbody").find("tr").eq(index + 1).find("input").focus();
                }
            });
        }

        function SetAssistControl(value, row, index) {
            var strHtml = "";
            if (row.assist) {
                strHtml = "<input type='checkbox' checked onclick='UpdateResultAssist(this)'/>";
            } else {
                strHtml = "<input type='checkbox' onclick='UpdateResultAssist(this)'/>";
            }

            return strHtml;
        }

        function UpdateResultAssist(obj) {
            var st = $(obj).prop("checked");
            var rowid = $(obj).parent().parent().attr('data-uniqueid');

            $.post("UpdateResultAssist", {
                ResultId: rowid,
                Assist: st
            }, function (data) {
                if (data.indexOf("成功") == -1) {
                    alert(data);
                }
            });
        }

        function SetRetestNo(value, row, index) {
            return value == '0' ? '0' : "<span class='btn-rfs badge badge-danger'>" + value + "</span>";
        }

        function SetUsageRecord(value, row, index) {
            if (value != null)
                return strHtml = "<button class='table-cell-button btn-rfs' value='" + value + "'  title='下载数据文件' onclick='DowloadFile(this)'><i class='bi-file-text'></i></button>";
            return '-';
        }

        function DowloadFile(obj) {
            DownloadFile($(obj).val());
        }

        function LoadSingleResult(id) {
            $('#SampleManager-TabAnalyts').bootstrapTable('destroy');
            var columns = [{ field: "id", visible: false }, { field: "sampleId", width: "10%" }, { field: "sampleNumber" }, 
            { field: "testName" }, { field: "analytesCode" },{ field: "analytesName" }, { field: "roundingOff" },
            { field: "lowA" }, { field: "lowB" }, { field: "standardValue" }, { field: "highB" }, { field: "highA" },
            { field: "original" }, { field: "units" }, { field: "final" }, { field: "fUnits", formatter: SetFUnits }, { field: "conclusion" },
            { field: "retestNo", formatter: SetRetestNo },{ field: "detectionTimes" }, { field: "testTimes" },
            { field: "entryModel" }, { field: "eqId" }, { field: "eqUsageRecordId", formatter: SetUsageRecord }, { field: "entryPersonName" },
            { field: "entryTime", formatter: SetTime },{ field: "status", width: "3%", align: "center", formatter: SetStatus },
            { field: "assist", width: "5%", align: "center", formatter: SetAssistControl }];

            $('#SampleManager-TabAnalyts').bootstrapTable({
                method: 'post',
                url: "GetSampleResults",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        Id: id
                    };
                    return data;
                },
                queryParamsType: "search", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                locale: "zh-CN",
                columns: columns,
                onClickRow: function (row, $element, field) {
                    $("#SampleManager-TabAnalyts").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    ResultId = row.id;
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#SampleManager-TabAnalyts').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        function SetFUnits(value, row, index) {
            if (row.status == null) {  // 根据状态判断分析项是否已录入结果，如果没有录入结果则不提供单位转换
                return value;
            } else { 
                return "<select class='table-cell-select' onclick='GetConverUnits(this,event)' onchange='UpdateFinal(this)'> <option>" + value + "</option></select>";
            }
        }

        function GetConverUnits(obj,event) {
            var rowid = $(obj).parent().parent().attr('data-uniqueid');
            var row = $('#SampleManager-TabAnalyts').bootstrapTable('getRowByUniqueId', rowid);

            if ($(obj).find('option').length > 1) { return; }
            $.post("/Common/GetConversionFactorList", {
                SourceCode: row.units
            }, function (data) {
                $(obj).empty();
                for (var i = 0; i < data.length; i++) {
                    $(obj).append(new Option(data[i].targetCode, data[i].factor));
                }
                $(obj).append(new Option(row.units, 1));
            });
            
        }

        function UpdateFinal(obj) {
            var rowid = $(obj).parent().parent().attr('data-uniqueid');
            var row = $('#SampleManager-TabAnalyts').bootstrapTable('getRowByUniqueId', rowid);

            $.post("UpdateResultFinalByFactor", {
                ResultId: rowid,
                Factor: $(obj).val(),
                Funits: $(obj).find("option:selected").text()
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    alert("更新成功请刷新表格数据！");
                } else {
                    alert(data);
                }
            });
        }
        
        function SetTime(value, row, index) {
            return value == null ? '-' : value.replace('T', ' ').split('.')[0];
        }

        function SetHStatus(value, row, index) {
            return value == "退回" ? "<span class='btn-rfs badge badge-danger'>重测</span>" : value;
        }

        function LoadHistory(sampleId) {
            if (sampleId == "") { return; }

            $('#SampleManager-History').bootstrapTable('destroy');
            var columns = [{ field: "sampleId" }, { field: "stepName" }, { field: "status", formatter: SetHStatus },
            { field: "recordTime", formatter: SetTime }, { field: "currentHandlerType" }, /*{ field: "currentHandlerId" },*/
            { field: "handlingTime", formatter: SetTime }, { field: "suggestion" }];

            $('#SampleManager-History').bootstrapTable({
                method: 'post',
                url: "GetSampleHistory",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        SampleId: sampleId
                    };
                    return data;
                },
                queryParamsType: "search", // 服务器端分页，这个参数必需是“limit”
                /*uniqueId: "id",*/          // 设置主键
                locale: "zh-CN",
                columns: columns,
                onClickRow: function (row, $element, field) {
                    $("#SampleManager-History").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#SampleManager-History').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        function LoadSampleBatch(batchid) {
            $('#SampleManager-BatchTable').bootstrapTable('destroy');
            var columns = [{ field: "id", visible: false }, { field: "batchId", width: "20%" }, { field: "ex1" },
            { field: "ex2" }, { field: "ex3" }, { field: "sendDepartment" }, { field: "sendPerson" }, { field: "sendTime", formatter: SetTime },
            { field: "personName" }, { field: "creatTime", formatter: SetTime }];

            $('#SampleManager-BatchTable').bootstrapTable({
                method: 'post',
                url: "GetSampleBatch",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        BatchId: batchid
                    };
                    return data;
                },
                dataField: "rows",
                uniqueId: "id",          // 设置主键
                locale: "zh-CN",
                columns: columns,
                onClickRow: function (row, $element, field) {
                    $("#SampleManager-BatchTable").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#SampleManager-BatchTable').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        function CreatMultiReport(obj) {

            var batchId = GetSelectIdByColor('SampleManager-BatchTable');
            if (batchId.length == 0) {
                alert('请先选择需要出报告的批次号！');
                return;
            }

            $.post("CreatMultiReport", {
                SampleBatchId: batchId,
                ReportName: $(obj).text()
            }, function (data) {
                alert(data);
            });
        }

        $("#SampleManager-Current").change(function () {
            var batchid = $("#SampleManager-Current").find("option:selected").text();
            LoadSamples("批次号", batchid);
            LoadSampleBatch(batchid);
        });

        $("#SampleManager-Abandon").click(function () {
            var ids = GetSelectItems('SampleManager-Table');
            if (ids == null) {
                alert('请先选择要废弃的样品');
                return;
            }

            $("#SampleManager-Abandon-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#modal-DoAbandon").click(function () {
            var ids = GetSelectItems('SampleManager-Table');
            var suggestion = $("#modal-Suggestion").val();
            suggestion = $.trim(suggestion);
            if (suggestion.length == 0) {
                alert("请输入废弃样品原因！");
            }

            $.post("DoAbandon", {
                SampleIds: ids,
                Suggestion: suggestion
            }, function (data) {
                SampleId == "";
                alert(data);
                $("#SampleManager-Abandon-Modal").modal('hide');
            });
        });

        $("#SampleManager-Examine").click(function () {
            var ids = GetSelectItems("SampleManager-Table");
            if (ids == null) {
                return false;
            }

            $.post("DoSampleManager", {
                Ids: ids
            }, function (data) {
                alert(data);
            });
        });

        $("#SampleManager-Review").click(function () {
            var id = ResultId;
            ResultId = "";
            if (id == "") {
                alert('请先选择需要重测的分析项！');
                return;
            }

            $.post("Review", {
                ResultId: id
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    var sampleid = data.split(',');
                    LoadSingleResult(sampleid[1])
                } else {
                    alert(data);
                }
            });
        });

        $("#SampleManager-Report").click(function () {
            if (SampleId == "") {
                alert('请先选择需要出报告的样品！');
                return;
            }

            $.post("CreateSingleReport", {
                SampleId: SampleId
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    alert("报告已生成，请在附件管理中查看！");
                } else {
                    alert(data);
                }
            });
        });

        $("#SampleManager-Export").click(function () {
            var ids = GetSelectItems("SampleManager-Table");
            if (ids == null) {
                alert("请先选择需要导出的样品！");
                return;
            }

            $.post("SampleToExcel", {
                Ids: ids
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    var fileName = data.split(',')[1];
                    var mime = getMIMEType(fileName);

                    var formData = new FormData();
                    formData.append("pathName", "ExportSampleExcel_Path");
                    formData.append("fileName", fileName);
                    formData.append("isTemp", "true");

                    const xhr = new XMLHttpRequest();
                    xhr.open('POST', '/Common/DownloadFile', true);
                    xhr.responseType = "blob";  // 这个是关键
                    xhr.onload = () => {
                        //'\ufeff' 这个变量是定义字符集为utf-8, 防止中文乱码的问题。此处代码下载EXCEL文件正确，不知道其它地方是不是正确。
                        const blob = new Blob([xhr.response], { type: mime + ",charset=utf-8"});
                        const blobUrl = URL.createObjectURL(blob);
                        const a = document.createElement('a');
                        a.style.display = 'none';
                        a.href = blobUrl;
                        a.download = "样品信息.xlsx"; // 文件名
                        a.target = '_blank';
                        a.click();
                    }
                    xhr.send(formData);
                } else {
                    alert(data);
                }
            });
        });

        $("#SampleManager-Import").click(function () {
            $("#SampleManager-Modal-Title").text("导入样品信息");

            $("#SampleManager-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("/Common/UploadDisposable", {
                Operation: "ImportSamples",
                ModalID: "SampleManager-Modal"
            }, function (datas) {
                $("#SampleManager-Modal-Embed").html(datas);
            });
            event.stopPropagation();
        });

        $("#li-tabAnalyte").hover(
            function () {
                if ($(this).attr("class").indexOf('tab-first-active') > 0) {
                    $(this).addClass('tab-enter-mouse');
                }
                if ($(this).attr("class").indexOf('tab-first-noactive') > 0) {
                    $(this).removeClass('tab-first-noactive');
                    $(this).addClass('tab-first-noactive-mouse');
                }
            },
            function () {
                $(this).removeClass('tab-enter-mouse');
                $(this).removeClass('tab-first-noactive-mouse');

                if ($(this).attr("data-active") == 'true') {
                    $(this).addClass('tab-first-active');
                }
                if ($(this).attr("data-active") == 'false') {
                    $(this).addClass('tab-first-noactive');
                }
            });

        $("#a-tabAnalyte").click(function () {
            if ($("#li-tabHistory").attr("class").indexOf('tab-other-active') > 0) {
                $("#li-tabHistory").removeClass('tab-other-active');
                $("#li-tabHistory").addClass('tab-other-noactive');
            }

            $("#li-tabAnalyte").attr("data-active", true);
            $("#li-tabHistory").attr("data-active", false);
        });

        $("#li-tabHistory").hover(
            function () {
                if ($(this).attr("class").indexOf('tab-other-active') > 0) {
                    $(this).addClass('tab-enter-mouse');
                }
                if ($(this).attr("class").indexOf('tab-other-noactive') > 0) {
                    $(this).removeClass('tab-other-noactive');
                    $(this).addClass('tab-other-noactive-mouse');
                }
            },
            function () {
                $(this).removeClass('tab-enter-mouse');
                $(this).removeClass('tab-other-noactive-mouse');

                if ($(this).attr("data-active") == 'true') {
                    $(this).addClass('tab-other-active');
                }
                if ($(this).attr("data-active") == 'false') {
                    $(this).addClass('tab-other-noactive');
                }
            });

        $("#a-tabHistory").click(function () {
            if ($("#li-tabAnalyte").attr("class").indexOf('tab-first-active') > 0) {
                $("#li-tabAnalyte").removeClass('tab-first-active');
                $("#li-tabAnalyte").addClass('tab-first-noactive');
            }

            $("#li-tabHistory").attr("data-active", true);
            $("#li-tabAnalyte").attr("data-active", false);

            LoadHistory(SampleId);
        });

    </script>
}
